package Chess.Moves;

import Chess.*;
import Chess.Pieces.*;
import Chess.Pieces.Piece.*;

/**
 *	A class representing a CaptureMove in Chess.
 *
 *	@author		Jeff Chan
 */
public class CaptureMove extends Move {

	private Piece opposingPiece;
	
	private CaptureMove(Piece piece, Square startSquare, Square endSquare, Piece opposingPiece) {
		super(piece, startSquare, endSquare);
		this.opposingPiece = opposingPiece;
	}

	public static boolean canBeExecuted(Board board, Square startSquare, Square endSquare) {
		boolean canBeExecuted = false;
		
		if (board != null) {
			Piece piece = board.getPiece(startSquare);
			if (piece != null && piece.canAttack(board, startSquare, endSquare))
				canBeExecuted = true;
		}
		
		return canBeExecuted;
	}

	public static CaptureMove execute(Board board, Square startSquare, Square endSquare) {
		Piece piece = board.getPiece(startSquare);
		Piece opposingPiece = board.getPiece(endSquare);
		board.removePiece(endSquare);
		board.movePiece(piece, startSquare, endSquare);		
		piece.incrementMoveCount();
		return new CaptureMove(piece, startSquare, endSquare, opposingPiece);
	}
	
	public void undo(Board board) {
		piece.decrementMoveCount();
		board.movePiece(piece, endSquare, startSquare);
		board.putPiece(endSquare, opposingPiece);
	}

	public MoveType getMoveType() {
		return MoveType.CAPTURE;
	}

	public Piece getOpposingPiece() {
		return opposingPiece;
	}
}
